package com.product.system.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.SysPost;
import com.product.system.domain.SysPosteChar;
import com.product.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/post")
public class SysPostController {
    @Autowired
    SysPostService postService;


    @PostMapping("postlist")
    public List<SysPost> list(@RequestParam(name = "pageNum") Integer pageNum, @RequestBody SysPost sysPost) {

        List<SysPost> list = postService.list(sysPost);

        return  list;
    }
    @PostMapping("postlistall")
    public PageInfo<SysPost> postlistall(@RequestParam(name = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum,3);

        List<SysPost> list = postService.listAll();
        PageInfo<SysPost> info = new PageInfo<>(list);
        return  info;
    }

    @GetMapping("postlistec")
    public List postlistec() {


        List<SysPost> list = postService.postlistechart();
        return  list;
    }
    @GetMapping ("postlistechart")
    public List postlistechar() {


        List<SysPosteChar> list = postService.postlistechar();
        return  list;
    }

    @PostMapping("add")
    public HashMap<String, Object> add(@RequestBody SysPost sysPost) {
        Boolean save = postService.save(sysPost);
        HashMap<String, Object> map = new HashMap();

        if (save) {
            map.put("code",200);
            map.put("msg","添加成功");
        } else {
            map.put("code",500);
            map.put("msg","添加失败");
        }

        return map;
    }
    @GetMapping("excel")
    public  void excel(HttpServletResponse response) throws Exception {
        String fileName = URLEncoder.encode("产品信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SysPost> list = postService.listAll();
        EasyExcel.write(response.getOutputStream(), SysPost.class).sheet("产品信息").doWrite(list);


    }


    @PostMapping("update")
    public HashMap<String, Object> update(@RequestBody SysPost sysPost) {
        Boolean save = postService.update(sysPost);
        HashMap<String, Object> map = new HashMap();


        if (save) {
            map.put("code",200);
            map.put("msg","添加成功");
        } else {
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }

    @PostMapping("delete")
    public HashMap<String, Object> delete(@RequestBody SysPost sysPost) {
        if (sysPost.getStatus().equals("1")){
            sysPost.setStatus("0");
        }else{
            sysPost.setStatus("1");
        }
        Boolean save = postService.updatebyid(sysPost);
        HashMap<String, Object> map = new HashMap();

        if (save) {
            map.put("code",200);
            map.put("msg","添加成功");
        } else {
            map.put("code",500);
            map.put("msg","添加失败");
        }

        return map;
    }
}

